{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 微信自动群发祝福\n",
    "在知乎专栏里面找到一篇文章，[用python和itchat实现狗年微信群发拜年](https://zhuanlan.zhihu.com/p/33822531?utm_source=wechat_session&utm_medium=social)，可以给好友群发信息，感觉很优秀呀，感激弄下来试试。\n",
    "\n",
    "代码地址：[Windsmild/WechatHappyNewYear](https://github.com/Windsmild/WechatHappyNewYear)，优秀啊\n",
    "\n",
    "同时参考了[itchat的官网文档](http://itchat.readthedocs.io/zh/latest/)\n",
    "\n",
    "事先说一句话，慎发，有被删好友的危险。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "█\r"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import itchat\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义两个函数，按照需求需要设计一个随机函数，给不同的群发送不同的信息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "@itchat.msg_register(itchat.content.TEXT)\n",
    "def print_content(msg):\n",
    "    print(msg['Text'])\n",
    "\n",
    "\n",
    "def randomBless():\n",
    "    blessings = [\"狗年第一天，一心一意送你祝福：祝你事业发达第一，平安健康第一，婚姻美满第一，家庭和谐第一，赚钱发财第一，开心快乐第一，轻松悠闲都是第一。\",\n",
    "                 \"健康是最佳的礼物，知足是最大的财富，信心是最好的品德，关心是最真的问候，牵挂是最深的思念，祝福是最美的话语。祝新年快乐!平安幸福!\",\n",
    "                 \"愿你狗年万事顺，得意洋洋满面春!欢快的歌声尽情飘，温暖的春风暖心潮。万千的喜气多热闹，吉祥的日子要来到。发条短信问个好，财源广进吉星照。万事顺利开怀笑，狗年幸福乐逍遥。\",\n",
    "                 \"除夕夜，千里迢迢把家赶；心思念，酸甜苦辣在心间；年夜饭，平安吉祥共团圆；喜开颜，快乐吉祥永相伴。祝狗年除夕快乐，团团圆圆幸福满！\",\n",
    "                 \"拜年礼包新配方，包含：十分关心十分甜蜜十分健康十分好运十分快乐十分吉祥十分幸福十分如意十分美满。愿你除夕快乐，狗年幸福满溢！\",\n",
    "                 \"祝君新年好！新年新面貌！新年新心情！新年新开始！新年新运气！新朋旧友齐愿你，万事总如意，钱途千万里！\",\n",
    "                 \"狗年到，短信早，祝福绕，人欢笑，生活好，步步高，重环保，健康牢，多关照，新目标，加力跑，乐淘淘。新春祝你事事好，生活妙，工资高！\",\n",
    "                 \"新年祝你事业如日中天，恋人亲密无间，薪水上万成千，快乐无际无边，烦恼渺如云烟，逍遥胜似神仙!我买了二斤时尚，购了三斤浪漫，自制八斤快乐，从心底切下一吨关怀，做个狗年大礼送给你!\",\n",
    "                 \"新年我把好运送到，祝您抱着平安，拥着健康，揣着幸福，搂着温馨，携着快乐，牵着财运，拽着吉祥，迈入狗年，快乐度过每一天! \"]\n",
    "    return random.sample(blessings, 1)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面是一段测试，给一个人发信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "itchat.auto_login(hotReload=True)\n",
    "users = itchat.search_friends(name='文笔超好的男同学')\n",
    "print(users[0])\n",
    "okWord = u'祝我的好友'+users[0]['NickName']+':\\n'+ randomBless() + \"\\nMade by WindAI[红包][红包]🎉🎉\"\n",
    "\n",
    "# 注意，下次下面这句话一执行可就发出去了\n",
    "itchat.send(okWord,users[0]['UserName'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面这里是给好友群发信息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取自己的好友\n",
    "friendList = itchat.get_friends(update=True)[1:]\n",
    "num=0 # 好友人数\n",
    "for friend in friendList:\n",
    "    num=num+1\n",
    "    print('正在给第'+str(num)+'个好友发送信息: '+friend['NickName'])\n",
    "    result=False\n",
    "    okWord = u'祝我的好友'+friend['NickName']+':\\n'+ randomBless() + \"\\nMade by WindAI[红包][红包]🎉🎉\"\n",
    "    result=itchat.send(okWord, friend['UserName'])\n",
    "    time.sleep(10)\n",
    "    if(result):\n",
    "        print('发送成功')\n",
    "    else:\n",
    "        print('发送失败')\n",
    "print('completed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "下面是给群，群发消息的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "itchat.auto_login(hotReload=True)\n",
    "groupList = itchat.get_chatrooms(update=True)\n",
    "num=0\n",
    "for group in groupList:\n",
    "    num=num+1\n",
    "    print('正在给第'+str(num)+'个群发送信息: '+group['NickName'])\n",
    "    result=False\n",
    "    okWord = u'祝群'+friend['NickName']+'里面的各位:\\n'+ randomBless() + \"\\nMade by WindAI[红包][红包]🎉🎉\"\n",
    "    result=itchat.send(okWord, group['UserName'])\n",
    "    time.sleep(10)\n",
    "    if(result):\n",
    "        print('发送成功')\n",
    "    else:\n",
    "        print('发送失败')\n",
    "print('completed')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
